package com.uzai.console.web.tencent;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.uzai.common.exception.BusinessException;
import com.uzai.common.result.ResponseCode;
import com.uzai.console.dto.tencent.TencentCosConfigDto;
import com.uzai.console.mapper.MerBaseConfigMapper;
import com.uzai.console.service.tencent.TencentCosService;
import com.uzai.console.vo.base.SelectKeyVo;
import com.uzai.console.vo.tencent.TencentCosUploadFileVo;
import com.uzai.console.web.BaseController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * 腾讯云转链
 * @author liuqi
 * @date 2021年08月16日 15:11
 */
@Api(value = "腾讯云",tags = "系统设置|腾讯云")
@ApiSort(29)
@RestController
public class TencentCosController extends BaseController {
    private Logger logger = LoggerFactory.getLogger(TencentCosController.class);

    @Autowired
    private TencentCosService tencentCosService;
    @Autowired
    private MerBaseConfigMapper merBaseConfigMapper;

    @ApiOperation(value = "验证腾讯云配置", notes = "验证腾讯云配置")
    @ApiOperationSupport(order = 1)
    @PostMapping("/tencentCos/checkConfig")
    public Object checkConfig(@RequestBody TencentCosConfigDto tencentCosConfigDto){
        Boolean ckeckFlag = tencentCosService.checkConfig(tencentCosConfigDto);
        if(ckeckFlag){
            throw new BusinessException(ResponseCode.SUCCESS, "验证正确");
        }else{
            throw new BusinessException(ResponseCode.BUSINESS_EXCEPTION, "配置错误");
        }
    }

    @ApiOperation(value = "保存腾讯云配置", notes = "保存腾讯云配置")
    @ApiOperationSupport(order = 2)
    @PostMapping("/tencentCos/addTencentCosConfig")
    public Object addTencentCosConfig(@RequestBody TencentCosConfigDto tencentCosConfigDto){
        tencentCosService.addTencentCosConfig(tencentCosConfigDto);
        return true;
    }

    @ApiOperation(value = "上传本地图片", notes = "上传本地图片", response = TencentCosUploadFileVo.class)
    @ApiOperationSupport(order = 3)
    @PostMapping("/tencentCos/uploadImage")
    public Object uploadImage(MultipartFile file){
        Long merId = getLoginInfo().getMerId();
        TencentCosUploadFileVo tencentCosUploadFileVo = new TencentCosUploadFileVo();
        String fileUrl =  tencentCosService.uploadFile(file, merId); //上传图片
        //String fileUrl =  tencentCosService.uploadFileAndTrans(file, merId, "gif"); //上传图片并转化成gif图片
        tencentCosUploadFileVo.setFileUrl(fileUrl);
        String fileTag = "[IMG]" + fileUrl + "[/IMG]";
        tencentCosUploadFileVo.setFileTag(fileTag);
        tencentCosUploadFileVo.setFilename(file.getOriginalFilename());
        return tencentCosUploadFileVo;
    }

    @ApiOperation(value = "上传本地视频", notes = "上传本地视频", response = TencentCosUploadFileVo.class)
    @ApiOperationSupport(order = 4)
    @PostMapping("/tencentCos/uploadVideo")
    public Object uploadVideo(MultipartFile file){
        Long merId = getLoginInfo().getMerId();
        TencentCosUploadFileVo tencentCosUploadFileVo = new TencentCosUploadFileVo();
        String fileUrl =  tencentCosService.uploadFile(file, merId); //上传视频
        tencentCosUploadFileVo.setFileUrl(fileUrl);
        String fileTag = "[VIDEO]" + fileUrl + "[/VIDEO]";
        tencentCosUploadFileVo.setFileTag(fileTag);
        tencentCosUploadFileVo.setFilename(file.getOriginalFilename());
        return tencentCosUploadFileVo;
    }

    @ApiOperation(value = "查询腾讯云配置详情", notes = "查询腾讯云配置详情", response = TencentCosConfigDto.class)
    @ApiOperationSupport(order = 5)
    @PostMapping("/tencentCos/findTencentCosConfigInfo")
    public Object findTencentCosConfigInfo(){
        Long merId = getLoginInfo().getMerId();
        return tencentCosService.findTencentCosConfigInfo(merId);
    }

    @ApiOperation(value = "查询腾讯云区域列表", notes = "查询腾讯云区域列表", response = SelectKeyVo.class)
    @ApiOperationSupport(order = 6)
    @PostMapping("/tencentCos/findRegionList")
    public Object findRegionList(){
        return tencentCosService.findRegionList();
    }

}
